package com.eedi.framework.notify.service;

import com.google.common.annotations.VisibleForTesting;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.exception.util.ServiceExceptionUtil;
import com.eedi.framework.notify.dal.dataobject.SysNotifyTemplateDO;
import com.eedi.framework.notify.enums.SysNoticeErrorCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 站内信发送 Service 实现类
 *
 * @author xrcoder
 */
@Service
@Validated
@Slf4j
public class SysNotifySendServiceImpl implements SysNotifySendService {

    @Resource
    private SysNotifyTemplateService sysNotifyTemplateService;

    @Resource
    private SysNotifyMessageService sysNotifyMessageService;

    @Override
    public String sendSingleNotifyToAdmin(String userId, String templateCode, Map<String, Object> templateParams) {
        return sendSingleNotify(userId, UserTypeEnum.MEMBER_SYS, templateCode, templateParams);
    }

    @Override
    public String sendSingleNotifyToMember(String userId, String templateCode, Map<String, Object> templateParams) {
        return sendSingleNotify(userId, UserTypeEnum.MEMBER_SYS, templateCode, templateParams);
    }

    @Override
    public String sendSingleNotify(String userId, UserTypeEnum userType, String templateCode, Map<String, Object> templateParams) {
        // 校验模版
        SysNotifyTemplateDO template = validateNotifyTemplate(templateCode);
        if (Objects.equals(template.getSysNotifyStatus(), CommonStatusEnum.DISABLE.getCode())) {
            log.info("[sendSingleNotify][模版({})已经关闭，无法给用户({}/{})发送]", templateCode, userId, userType);
            return null;
        }
        // 校验参数
        validateTemplateParams(template, templateParams);

        // 发送站内信
        String content = sysNotifyTemplateService.formatNotifyTemplateContent(template.getSysNotifyContent(), templateParams);
        return sysNotifyMessageService.createNotifyMessage(userId, userType, template, content, templateParams);
    }

    @VisibleForTesting
    public SysNotifyTemplateDO validateNotifyTemplate(String templateCode) {
        // 获得站内信模板。考虑到效率，从缓存中获取
        SysNotifyTemplateDO template = sysNotifyTemplateService.getNotifyTemplateByCodeFromCache(templateCode);
        // 站内信模板不存在
        if (template == null) {
            throw ServiceExceptionUtil.exception(SysNoticeErrorCodeConstants.SYS_NOTICE_NOT_FOUND);
        }
        return template;
    }

    /**
     * 校验站内信模版参数是否确实
     *
     * @param template 邮箱模板
     * @param templateParams 参数列表
     */
    @VisibleForTesting
    public void validateTemplateParams(SysNotifyTemplateDO template, Map<String, Object> templateParams) {
        template.getSysNotifyParams().forEach(key -> {
            Object value = templateParams.get(key);
            if (value == null) {
                throw ServiceExceptionUtil.exception(SysNoticeErrorCodeConstants.SYS_NOTIFY_SEND_TEMPLATE_PARAM_MISS, key);
            }
        });
    }
}
